<%@ page contentType="text/html;charset=UTF-8" %>
<script>
$(document).ready(function() {
	$('#outboundTable').bootstrapTable({
		 
		  //请求方法
               method: 'get',height:550,
               //类型json
               dataType: "json",
               //显示刷新按钮
               showRefresh: true,
               //显示切换手机试图按钮
               showToggle: true,
               //显示 内容列下拉框
    	       showColumns: true,
    	       //显示到处按钮
    	       showExport: true,
    	       //显示切换分页按钮
    	       showPaginationSwitch: true,
    	       //显示详情按钮
    	       detailView: true,
    	       	//显示详细内容函数
	           detailFormatter: "detailFormatter",
    	       //最低显示2行
    	       minimumCountColumns: 2,
               //是否显示行间隔色
               striped: true,
               //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）     
               cache: false,    
               //是否显示分页（*）  
               pagination: true,   
                //排序方式 
               sortOrder: "asc",  
               //初始化加载第一页，默认第一页
               pageNumber:1,   
               //每页的记录行数（*）   
               pageSize: 10,  
               //可供选择的每页的行数（*）    
               pageList: [10, 25, 50, 100],
               //这个接口需要处理bootstrap table传递的固定参数,并返回特定格式的json数据  
               url: "${ctx}/wms/outbound/data",
               //默认值为 'limit',传给服务端的参数为：limit, offset, search, sort, order Else
               //queryParamsType:'',   
               ////查询参数,每次调用是会带上这个参数，可自定义                         
               queryParams : function(params) {
               	var searchParam = $("#searchForm").serializeJSON();
               	searchParam.pageNo = params.limit === undefined? "1" :params.offset/params.limit+1;
               	searchParam.pageSize = params.limit === undefined? -1 : params.limit;
               	searchParam.orderBy = params.sort === undefined? "" : params.sort+ " "+  params.order;
                   return searchParam;
               },
               //分页方式：client客户端分页，server服务端分页（*）
               sidePagination: "server",
               contextMenuTrigger:"right",//pc端 按右键弹出菜单
               contextMenuTriggerMobile:"press",//手机端 弹出菜单，click：单击， press：长按。
               contextMenu: '#context-menu',
               onContextMenuItem: function(row, $el){
                   if($el.data("item") == "edit"){
                   	edit(row.id);
                   } else if($el.data("item") == "delete"){
                        jp.confirm('确认要删除该出库单记录吗？', function(){
                       	jp.loading();
                       	jp.get("${ctx}/wms/outbound/delete?id="+row.id, function(data){
                   	  		if(data.success){
                   	  			$('#outboundTable').bootstrapTable('refresh');
                   	  			jp.success(data.msg);
                   	  		}else{
                   	  			jp.error(data.msg);
                   	  		}
                   	  	})
                   	   
                   	});
                      
                   } 
               },
              
               onClickRow: function(row, $el){
               },
        columns: [{
            checkbox: true

        }
            ,{
                field: 'code',
                title: '出库单号',
                sortable: true
                ,formatter:function(value, row , index){
                    return "<a href='javascript:view(\""+row.id+"\")'>"+value+"</a>";
                }

            }
            ,{
                field: 'shipmentNumbers',
                title: '装运单号',
                sortable: true

            }
            ,{
                field: 'erpOrder',
                title: '提货单号',
                sortable: true

            }
            ,{
                field: 'status',
                title: '状态',
                sortable: true

            }
            ,{
                field: 'type',
                title: '出库单类型',
                sortable: true

            }
            ,{
                field: 'workType',
                title: '作业类型',
                sortable: true

            }
            ,{
                field: 'date',
                title: '订单日期',
                sortable: true
                ,formatter:function(value, row , index){
                    return value.toString().substring(0,16)
                }
            }

            ,{
                field: 'amount',
                title: '总订购量',
                sortable: true

            }



            ,{
                field: 'plateNumber',
                title: '车牌号码',
                sortable: true

            }
            ,{
                field: 'driverPhone',
                title: '司机电话',
                sortable: true

            }

            ,{
                field: 'unitName',
                title: '收货单位名称',
                sortable: true

            }
            ,{
                field: 'unitConfirmor',
                title: '收货单位确认人',
                sortable: true

            }
            ,{
                field: 'phone',
                title: '确认人电话',
                sortable: true

            }
            ,{
                field: 'site',
                title: '收货详细地址',
                sortable: true

            }
            ,{
                field: 'arriveProvince',
                title: '到站省',
                sortable: true

            }
            ,{
                field: 'arriveCity',
                title: '到站市',
                sortable: true

            }
            ,{
                field: 'distance',
                title: '合同里程',
                sortable: true

            }
            ,{
                field: 'transitLines',
                title: '运输线路',
                sortable: true

            },{
                field: 'grossWeight',
                title: '总毛重',
                sortable: true

            }
            ,{
                field: 'volume',
                title: '总体积',
                sortable: true

            }

            ,{
                field: 'netWeight',
                title: '总净重',
                sortable: true

            }
            ,{
                field: 'remarks',
                title: '备注信息',
                sortable: true

            }
        ]
		
		});
		
		  
	  if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端

		 
		  $('#outboundTable').bootstrapTable("toggleView");
		}
	  
	  $('#outboundTable').on('check.bs.table uncheck.bs.table load-success.bs.table ' +
                'check-all.bs.table uncheck-all.bs.table', function () {
            $('#remove').prop('disabled', $('#outboundTable').bootstrapTable('getSelections').length!=1);
            $('#dayin').prop('disabled', $('#outboundTable').bootstrapTable('getSelections').length!=1);
            $('#edit').prop('disabled', $('#outboundTable').bootstrapTable('getSelections').length!=1);
          $('#edit1').prop('disabled', $('#outboundTable').bootstrapTable('getSelections').length!=1);

          $('#qx').prop('disabled', $('#outboundTable').bootstrapTable('getSelections').length!=1);
        });
		$("#btnImport").click(function(){
			jp.open({
			    type: 1, 
			    area: [500, 300],
			    title:"导入数据",
			    content:$("#importBox").html() ,
			    btn: ['下载模板','确定', '关闭'],
				    btn1: function(index, layero){
					  window.location='${ctx}/wms/outbound/import/template';
				  },
			    btn2: function(index, layero){
				        var inputForm =top.$("#importForm");
				        var top_iframe = top.getActiveTab().attr("name");//获取当前active的tab的iframe 
				        inputForm.attr("target",top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示
				        inputForm.onsubmit = function(){
				        	jp.loading('  正在导入，请稍等...');
				        }
				        inputForm.submit();
					    jp.close(index);
				  },
				 
				  btn3: function(index){ 
					  jp.close(index);
	    	       }
			}); 
		});
		    
	  $("#search").click("click", function() {// 绑定查询按扭
		  $('#outboundTable').bootstrapTable('refresh');
		});
	 
	 $("#reset").click("click", function() {// 绑定查询按扭
		  $("#searchForm  input").val("");
		  $("#searchForm  select").val("");
		   $("#searchForm  .select-item").html("");
		  $('#outboundTable').bootstrapTable('refresh');
		});
		
		
	});
		
  function getIdSelections() {
        return $.map($("#outboundTable").bootstrapTable('getSelections'), function (row) {
            return row.id
        });
    }
function gettypeSelections() {
    return $.map($("#outboundTable").bootstrapTable('getSelections'), function (row) {
        return row.type
    });
}
function q() {
    jp.confirm('确认要取消该订单的出库操作吗？', function(){
    	var status= p();
        if(status=="已创建"){
            jp.warning("该订单没有进行出库操作！")
        }else{
              $.ajax({
				  url:"${ctx}/wms/outbound/qxck?ids=" + getIdSelections(),
				  success:function (data) {
					  jp.success("取消成功");
                      $('#outboundTable').bootstrapTable('refresh');
                  }
			  })
        }
    })
  }
  function deleteAll(){
      var status= p();
      if(status!="已创建"){
          jp.warning("该订单已出库不能作废！")
          return;
      }
		jp.confirm('确认要删除该出库单记录吗？', function(){
			jp.loading();  	
			jp.get("${ctx}/wms/outbound/deleteAll?ids=" + getIdSelections(), function(data){
         	  		if(data.success){
         	  			$('#outboundTable').bootstrapTable('refresh');
         	  			jp.success(data.msg);
         	  		}else{
         	  			jp.error(data.msg);
         	  		}
         	  	})
          	   
		})
  }
  
  function add(){
	  jp.openDialog('新增出库单', "${ctx}/wms/outbound/form",'1400px', '1200px', $('#outboundTable'));
  }
  function view(id) {
      jp.openDialogView('查看出库单', "${ctx}/wms/outbound/form?id=" + id,'1400px', '1200px', $('#outboundTable'));
  }
function edit1(id) {
    jp.openDialog('查看出库单', "${ctx}/wms/outbound/form3?id=" + getIdSelections(),'1400px', '1200px', $('#outboundTable'));
}
  function edit(id){//没有权限时，不显示确定按钮
  	  if(id == undefined){
			id = getIdSelections();
		}
      var status= p();
      if(status!="已创建"){
          jp.warning("该订单已出库不能修改！");
          return;
      }
      if(gettypeSelections().toString()=="销售出库"){
  	  	jp.warning("此单据不支持修改！");
  	  	return;
	  }
	  jp.openDialog('编辑出库单', "${ctx}/wms/outbound/form?id=" + id,'1400px', '1200px', $('#outboundTable'));


  }



function dayin(){
    if(getIdSelections()!=null && getIdSelections() !=""){
        if(p()=="拣货完成")
            jp.openDialogView('打印', "${ctx}/wms/outbound/dayin?ids=" + getIdSelections(),'800px', '800px');
    }


}

  function detailFormatter(index, row) {
	  var htmltpl =  $("#outboundChildrenTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
	  var html = Mustache.render(htmltpl, {
			idx:row.id
		});
	  $.get("${ctx}/wms/outbound/detail?id="+row.id, function(outbound){

    	var outboundChild1RowIdx = 0, outboundChild1Tpl = $("#outboundChild1Tpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
    	var data1 =outbound.storehouseList;
		for (var i=0; i<data1.length; i++){
			addRow('#outboundChild-'+row.id+'-1-List', outboundChild1RowIdx, outboundChild1Tpl, data1[i]);
			outboundChild1RowIdx = outboundChild1RowIdx + 1;
		}

    	var outboundChild2RowIdx = 0, outboundChild2Tpl = $("#outboundChild2Tpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
		var data2 =outbound.outStatisticsList;
		for (var i=0; i<data2.length; i++){
			addRow('#outboundChild-'+row.id+'-2-List', outboundChild2RowIdx, outboundChild2Tpl, data2[i]);
			outboundChild2RowIdx = outboundChild2RowIdx + 1;
		}


      })

        return html;
    }

	function addRow(list, idx, tpl, row){
		$(list).append(Mustache.render(tpl, {
			idx: idx, delBtn: true, row: row
		}));
	}

        function p() {
            return $.map($("#outboundTable").bootstrapTable('getSelections'), function (row) {
                return row.status
            });
        }





</script>
<script type="text/template" id="outboundChildrenTpl">//<!--
	<div class="tabs-container">
		<ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#tab-{{idx}}-1" aria-expanded="true">出库物料明细</a></li>
		</ul>
		<div class="tab-content">
				 <div id="tab-{{idx}}-2" class="tab-pane fade ">
						<table class="ani table">
						<thead>
							<tr>
								<th>职位</th>
								<th>姓名</th>
								<th>工作量</th>
								<th>备注信息</th>
							</tr>
						</thead>
						<tbody id="outboundChild-{{idx}}-2-List">
						</tbody>
					</table>
				</div>
				<div id="tab-{{idx}}-1" class="tab-pane fade in active">
					<table class="ani table">
						<thead>
							<tr>
								<th>物料名称</th>
								<th>物料编码</th>
								<th>包装</th>
								<th>单位</th>
								<th>订货数量</th>
								<th>拣货数量</th>
							</tr>
						</thead>
						<tbody id="outboundChild-{{idx}}-1-List">
						</tbody>
					</table>
				</div>
		</div>//-->
	</script>
	<script type="text/template" id="outboundChild2Tpl">//<!--
				<tr>
					<td>
						{{row.position.name}}
					</td>
					<td>
						{{row.name}}
					</td>
					<td>
						{{row.work}}
					</td>
					<td>
						{{row.remarks}}
					</td>
				</tr>//-->
	</script>
	<script type="text/template" id="outboundChild1Tpl">//<!--
				<tr>
					<td>
						{{row.name.name}}
					</td>
					<td>
						{{row.code}}
					</td>
					<td>
						{{row.packing}}
					</td>
					<td>
						{{row.unit}}
					</td>

					<td>
						{{row.orderNumber}}
					</td>

					<td>
						{{row.picking}}
					</td>


				</tr>//-->
	</script>
